package com.llkj.qingnianguoxue.home;

import android.os.Bundle;
import android.text.SpannableString;
import android.view.View;
import android.widget.TextView;

import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.SignCalendarAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.bean.SignCalendarBean;
import com.llkj.qingnianguoxue.bean.SignInBean;
import com.llkj.qingnianguoxue.customview.ExpandGridView;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.mine.mycoin.CoinExchangeActivity;
import com.llkj.qingnianguoxue.util.CalendarUtil;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.StringUtils;
import com.llkj.qingnianguoxue.util.TextViewUtil;
import com.llkj.qingnianguoxue.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SignInActivity extends BaseActivity implements CalendarUtil.RefreshDateData {
    @BindView(R.id.tv_date)
    TextView tvDate;//日期
    @BindView(R.id.tv_score)
    TextView tvScore;//分数
    @BindView(R.id.gv_calendar)
    ExpandGridView gvCalendar;//日历
    @BindView(R.id.tv_sign_count)
    TextView tvSignCount;//连续签到次数
    @BindView(R.id.tv_sign)
    TextView btnSign;//签到按钮
    @BindView(R.id.tvScan)
    TextView btnScan;//兑换按钮

    private CalendarUtil calendarUtil;//日历工具
    private SignCalendarAdapter calendarAdapter;
    private List<SignCalendarBean.Calendar> dateData;
    private String mIntegral;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in);
        setTitle("签到", true, TITLE_TYPE_IMG, R.drawable.icon_back, true,TITLE_TYPE_IMG, R.drawable.icon_sign_note);
        ButterKnife.bind(this);

        initView();
    }

    private void initView() {
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        tvDate = (TextView) findViewById(R.id.tv_date);//日期
        tvScore = (TextView) findViewById(R.id.tv_score);//分数
        gvCalendar = (ExpandGridView) findViewById(R.id.gv_calendar);
        tvSignCount = (TextView) findViewById(R.id.tv_sign_count);//连续签到次数

        dateData = new ArrayList<>();
        calendarAdapter = new SignCalendarAdapter(this, dateData);
        gvCalendar.setAdapter(calendarAdapter);

        calendarUtil = new CalendarUtil(this);
        tvDate.setText(calendarUtil.getCurrYear()+"-"+calendarUtil.getCurrMonth()+"-"+calendarUtil.getDay());
    }

    @Override
    protected void rightDoWhat() {
        super.rightDoWhat();
        startActivity(SignRuleActivity.class);
    }

    @OnClick({R.id.tv_sign, R.id.tvScan})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.tv_sign:
                if(btnSign.isSelected()){
                    return;
                }
                loadWaitDialog();
                HttpMethod.indexRegister(this,this);
                break;
            case R.id.tvScan:
                String integral = "0".equals(mIntegral)?"-1":mIntegral;
                Bundle bundle = new Bundle();
                bundle.putString("integral",integral);
                startActivity(CoinExchangeActivity.class,bundle);
                break;
        }
    }
    @Override
    public void refreshDateData(int curStartIndex, int curEndIndex, int[] day, int[] date) {
        int size = date.length;
        if (date[size - 1] >= 7) {
            size = size - 7;
        }

        int start = 0;
        if (date[7] == 1) {
            start = 7;
        }

        SignCalendarBean.Calendar calendar = null;
        for (int i = start; i < size; i++) {
            calendar = new SignCalendarBean.Calendar();
            dateData.add(calendar);
            calendar.setDay(String.valueOf(date[i]));

            calendar.setIsSign(null);
//            if (i > curEndIndex - 4 && i < curEndIndex) {
//                calendar.setIsSign(String.valueOf(1));
//            }
            if (i < curStartIndex || i >= curEndIndex) {
                calendar.setDay(null);
            }
        }

        calendarAdapter.notifyDataSetChanged();

        HttpMethod.indexSignIn(this,this);
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_INDEX_SIGN_IN) {//2.3 用户签到
            SignInBean signInBean = GsonUtil.GsonToBean(data,SignInBean.class);
            if(signInBean != null){
                if("1".equals(signInBean.getIs_signin())){
                    btnSign.setSelected(true);
                }

                setSignNum(signInBean.getNum());

                mIntegral = signInBean.getIntegral();
                setIntegral();

                if(!StringUtils.isEmpty(signInBean.getPop_up())){
                    ToastUtil.makeLongText(this,"+"+signInBean.getPop_up()+" 范儿币");
                }

                if("1".equals(signInBean.getIs_dui())){
                    btnScan.setVisibility(View.VISIBLE);
                }

                SignCalendarBean.Calendar dayBean = null;
                List<String> signDays = signInBean.getDay();
                for(int i=0;i<dateData.size();i++){
                    dayBean = dateData.get(i);
                    if(signDays.contains(dayBean.getDay())){
                        dayBean.setIsSign("1");
                    }
                }
                calendarAdapter.notifyDataSetChanged();
            }
        }else if (method == HttpMethod.HTTP_INDEX_REGISTER) {//2.3 用户签到
            SignInBean signInBean = GsonUtil.GsonToBean(data,SignInBean.class);
            if(signInBean != null){
                if("1".equals(signInBean.getIs_signin())){
                    btnScan.setVisibility(View.VISIBLE);
                }
                setSignNum(signInBean.getNum());

                btnSign.setSelected(true);
                SignCalendarBean.Calendar dayBean = null;
                String today = String.valueOf(calendarUtil.getDay());
                for(int i=0;i<dateData.size();i++){
                    dayBean = dateData.get(i);
                    if(today.equals(dayBean.getDay())){
                        dayBean.setIsSign("1");
                    }
                }
                calendarAdapter.notifyDataSetChanged();

                int integral = Integer.parseInt(signInBean.getIntegral())- Integer.parseInt(mIntegral);
                ToastUtil.makeLongText(this,"+"+integral);
                mIntegral = signInBean.getIntegral();
                setIntegral();
            }
        }
    }
    private void setIntegral(){
        String score = mIntegral+" 范儿币";
        SpannableString scoreSpan = TextViewUtil.getForegroundColorSpan(this, score, 0, mIntegral.length(), getResources().getColor(R.color.colorFE4A49));
        tvScore.setText(scoreSpan);
    }

    private void setSignNum(String num){
        String count = "已连续签到"+num+"天";
        SpannableString signCount = TextViewUtil.setForegroundUnderLine(this, count, 5, 5+num.length(), getResources().getColor(R.color.colorFE4A49));
        tvSignCount.setText(signCount);
    }

    @Override
    protected void onhttpFailed(String message, int method) {
        super.onhttpFailed(message, method);
        if(!StringUtils.isEmpty(message)){
            ToastUtil.makeLongText(this,message);
        }
    }
}
